package otimista.threads;

import util.Table;

public class Producer implements Runnable {

	private final Table table;
	private final int iterations;

	public Producer(Table table, int iterations) {
		this.table = table;
		this.iterations = iterations;
	}
	
	@Override
	public void run() {
		try {
			addValues();
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
	}

	private void addValues() throws InterruptedException {
		int temp = 0;
		while(temp < this.iterations) {
			add();
			temp++;
		}
	}
	
	public void add() {
		synchronized (table) {
			int tam = table.getMap().size();
			table.putValue(tam, String.valueOf(tam));
		}
	}
}
